# -*- coding: utf-8 -*-
import numpy
from sklearn import datasets     #设置数据集
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier  #选择临近的点做邻居，模拟数据预测值
from six.moves import reload_module
from sklearn.model_selection import GridSearchCV

iris = datasets.load_iris()  #导入鸢尾花数据
iris_x = iris.data
iris_y = iris.target

from sklearn.model_selection import cross_val_score


k_scores = []
for k in range(1,31):
    knn = KNeighborsClassifier(n_neighbors=k)
    #loss = -cross_val_score(knn, iris_x, iris_y, cv=10, scoring="mean_squared_error")  #误差
    score = cross_val_score(knn,iris_x,iris_y,cv=5,scoring="accuracy")
    k_scores.append(score.mean())
    print(score.mean())

    #输出为   打印出了k组的成绩，这样可以更好的衡量模型的准确度，防止偏差
    # [ 0.96666667  1.          0.93333333  0.96666667  1.        ]



# GridSearchCV(cv=KNeighborsClassifier(),param_grid={'k':range(1,100)})
# # loss = -cross_val_score(knn, iris_x, iris_y, cv=10, scoring="mean_squared_error")  #误差
# #score = cross_val_score(knn,iris_x,iris_y,cv=10,scoring="accuracy")
# # k_scores.append(loss.mean())
# print(k_scores)